AWSTemplateFormatVersion: '2010-09-09'
Description: >
  EBS gp2 to gp3 Conversion Remediator.
  Purpose-specific, opt-in remediation module for identifying gp2 EBS volumes
  and optionally modifying eligible volumes to gp3. This template is separate
  from the cost optimization assessment/reporting template.

Parameters:
  ProjectName:
    Type: String
    Default: ebs-gp2-to-gp3-conversion-remediator
    Description: Base name used for created resources.

  TargetRegionsCsv:
    Type: String
    Default: all-enabled
    Description: Comma-separated AWS Regions to scan, or all-enabled to scan all enabled Regions.

  ExcludedRegionsCsv:
    Type: String
    Default: ap-northeast-3
    Description: Comma-separated AWS Regions to exclude from scanning/remediation.

  DryRun:
    Type: String
    Default: 'true'
    AllowedValues: ['true', 'false']
    Description: When true, only report what would be modified. When false, modify eligible volumes.

  RequireApprovalTag:
    Type: String
    Default: 'true'
    AllowedValues: ['true', 'false']
    Description: >
      When true, only modify gp2 volumes tagged with ApprovalTagKey=ApprovalTagValue.
      For broad backlog cleanup, set false only after reviewing dry-run results.

  ApprovalTagKey:
    Type: String
    Default: CostOptApproved
    Description: Tag key required when RequireApprovalTag is true.

  ApprovalTagValue:
    Type: String
    Default: 'true'
    Description: Tag value required when RequireApprovalTag is true.

  IgnoreTagKeysCsv:
    Type: String
    Default: CostOptIgnore,DoNotModify,DoNotDelete
    Description: Comma-separated tag keys that prevent modification when present, regardless of value.

  IgnoreTagPairsCsv:
    Type: String
    Default: CostOptIgnore=true,DoNotModify=true,DoNotDelete=true
    Description: Comma-separated key=value tag pairs that prevent modification when present.

  MinimumVolumeSizeGiB:
    Type: Number
    Default: 1
    Description: Only gp2 volumes at least this size are considered candidates.

  IncludeAttachedVolumes:
    Type: String
    Default: 'true'
    AllowedValues: ['true', 'false']
    Description: Include in-use attached gp2 volumes. Set false to only modify unattached volumes.

  IncludeUnattachedVolumes:
    Type: String
    Default: 'true'
    AllowedValues: ['true', 'false']
    Description: Include available unattached gp2 volumes.

  TargetIops:
    Type: Number
    Default: 3000
    Description: Target gp3 IOPS. 3000 is the gp3 baseline.

  TargetThroughput:
    Type: Number
    Default: 125
    Description: Target gp3 throughput in MiB/s. 125 is the gp3 baseline.

  PreserveHigherGp2Performance:
    Type: String
    Default: 'true'
    AllowedValues: ['true', 'false']
    Description: >
      When true, if a gp2 volume's size-based baseline IOPS is above TargetIops,
      the remediator uses the higher estimated gp2 baseline IOPS for gp3.

  MaxVolumesPerRun:
    Type: Number
    Default: 50
    Description: Safety limit. 0 means no limit. Recommended first cleanup batch is 25 to 100.

  ContinueOnAccessDenied:
    Type: String
    Default: 'true'
    AllowedValues: ['true', 'false']
    Description: Continue scanning other Regions/resources when blocked by IAM/SCP.

  ScheduleEnabled:
    Type: String
    Default: 'false'
    AllowedValues: ['true', 'false']
    Description: Whether to create an enabled EventBridge schedule for recurring runs.

  ScheduleExpression:
    Type: String
    Default: rate(30 days)
    Description: EventBridge schedule expression used when ScheduleEnabled is true.

Conditions:
  IsScheduleEnabled: !Equals [!Ref ScheduleEnabled, 'true']

Resources:
  RemediatorRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: !Sub '${ProjectName}-${AWS::AccountId}-${AWS::Region}-role'
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
            Action: sts:AssumeRole
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
      Policies:
        - PolicyName: EbsGp2ToGp3ConversionPolicy
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Sid: DiscoverRegions
                Effect: Allow
                Action:
                  - ec2:DescribeRegions
                Resource: '*'
              - Sid: ReadVolumesAndModifications
                Effect: Allow
                Action:
                  - ec2:DescribeVolumes
                  - ec2:DescribeVolumesModifications
                  - ec2:DescribeTags
                Resource: '*'
              - Sid: ModifyVolumes
                Effect: Allow
                Action:
                  - ec2:ModifyVolume
                Resource: '*'

  RemediatorFunction:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: !Ref ProjectName
      Description: Purpose-specific remediation Lambda for converting eligible EBS gp2 volumes to gp3.
      Runtime: python3.12
      Handler: index.handler
      Timeout: 900
      MemorySize: 512
      Role: !GetAtt RemediatorRole.Arn
      Environment:
        Variables:
          TARGET_REGIONS_CSV: !Ref TargetRegionsCsv
          EXCLUDED_REGIONS_CSV: !Ref ExcludedRegionsCsv
          DRY_RUN: !Ref DryRun
          REQUIRE_APPROVAL_TAG: !Ref RequireApprovalTag
          APPROVAL_TAG_KEY: !Ref ApprovalTagKey
          APPROVAL_TAG_VALUE: !Ref ApprovalTagValue
          IGNORE_TAG_KEYS_CSV: !Ref IgnoreTagKeysCsv
          IGNORE_TAG_PAIRS_CSV: !Ref IgnoreTagPairsCsv
          MINIMUM_VOLUME_SIZE_GIB: !Ref MinimumVolumeSizeGiB
          INCLUDE_ATTACHED_VOLUMES: !Ref IncludeAttachedVolumes
          INCLUDE_UNATTACHED_VOLUMES: !Ref IncludeUnattachedVolumes
          TARGET_IOPS: !Ref TargetIops
          TARGET_THROUGHPUT: !Ref TargetThroughput
          PRESERVE_HIGHER_GP2_PERFORMANCE: !Ref PreserveHigherGp2Performance
          MAX_VOLUMES_PER_RUN: !Ref MaxVolumesPerRun
          CONTINUE_ON_ACCESS_DENIED: !Ref ContinueOnAccessDenied
      Code:
        ZipFile: |
          import json
          import os
          import traceback
          from datetime import datetime, timezone

          import boto3
          from botocore.exceptions import ClientError

          def utc_now_iso():
              return datetime.now(timezone.utc).isoformat()

          def env_bool(name, default=False):
              value = os.environ.get(name, str(default)).strip().lower()
              return value in ('1', 'true', 'yes', 'y')

          def env_int(name, default=0):
              try:
                  return int(os.environ.get(name, str(default)).strip())
              except Exception:
                  return default

          def split_csv(value):
              if value is None:
                  return []
              return [item.strip() for item in str(value).split(',') if item.strip()]

          def parse_tag_pairs(value):
              pairs = {}
              for item in split_csv(value):
                  if '=' not in item:
                      continue
                  key, val = item.split('=', 1)
                  key = key.strip()
                  val = val.strip()
                  if key:
                      pairs[key] = val
              return pairs

          def tags_to_dict(tags):
              result = {}
              for tag in tags or []:
                  key = tag.get('Key')
                  if key:
                      result[key] = tag.get('Value', '')
              return result

          def get_enabled_regions(excluded_regions):
              requested = os.environ.get('TARGET_REGIONS_CSV', 'all-enabled').strip()
              if requested and requested.lower() != 'all-enabled':
                  regions = split_csv(requested)
              else:
                  ec2 = boto3.client('ec2', region_name=os.environ.get('AWS_REGION', 'us-east-1'))
                  response = ec2.describe_regions(AllRegions=False)
                  regions = sorted([r['RegionName'] for r in response.get('Regions', [])])
              return [r for r in regions if r not in excluded_regions]

          def has_ignore_tag(tag_map, ignore_tag_keys, ignore_tag_pairs):
              for key in ignore_tag_keys:
                  if key in tag_map:
                      return True, 'ignore_tag_key=' + key
              for key, value in ignore_tag_pairs.items():
                  if tag_map.get(key) == value:
                      return True, 'ignore_tag_pair=' + key + '=' + value
              return False, ''

          def has_approval_tag(tag_map, settings):
              if not settings['require_approval_tag']:
                  return True
              return tag_map.get(settings['approval_tag_key']) == settings['approval_tag_value']

          def estimated_gp2_baseline_iops(size_gib):
              try:
                  size = int(size_gib)
              except Exception:
                  return 3000
              return max(100, min(size * 3, 16000))

          def desired_gp3_iops(volume, settings):
              target = settings['target_iops']
              if settings['preserve_higher_gp2_performance']:
                  baseline = estimated_gp2_baseline_iops(volume.get('Size', 0))
                  return max(target, baseline)
              return target

          def volume_has_active_modification(ec2, volume_id):
              try:
                  response = ec2.describe_volumes_modifications(VolumeIds=[volume_id])
                  mods = response.get('VolumesModifications', [])
                  if not mods:
                      return False, None
                  state = mods[0].get('ModificationState')
                  if state in ('modifying', 'optimizing'):
                      return True, state
                  return False, state
              except ClientError as e:
                  code = e.response.get('Error', {}).get('Code', '')
                  if code in ('InvalidVolume.NotFound', 'InvalidVolumeModification.NotFound'):
                      return False, None
                  raise

          def scan_region(region, settings):
              ec2 = boto3.client('ec2', region_name=region)
              result = {
                  'region': region,
                  'evaluated': 0,
                  'gp2_volumes': 0,
                  'eligible': 0,
                  'would_modify': 0,
                  'modified': 0,
                  'skipped_non_gp2': 0,
                  'skipped_too_small': 0,
                  'skipped_attached_excluded': 0,
                  'skipped_unattached_excluded': 0,
                  'skipped_missing_approval_tag': 0,
                  'skipped_ignore_tag': 0,
                  'skipped_active_modification': 0,
                  'access_denied': 0,
                  'failed': 0,
                  'failures': [],
                  'candidates': [],
                  'modified_volumes': []
              }

              try:
                  paginator = ec2.get_paginator('describe_volumes')
                  pages = paginator.paginate()
              except ClientError as e:
                  code = e.response.get('Error', {}).get('Code', 'Unknown')
                  message = e.response.get('Error', {}).get('Message', str(e))
                  result['failures'].append({'operation': 'DescribeVolumes', 'error_code': code, 'error_message': message})
                  if code in ('AccessDenied', 'AccessDeniedException', 'UnauthorizedOperation') or 'explicit deny' in message.lower():
                      result['access_denied'] += 1
                  else:
                      result['failed'] += 1
                  if not settings['continue_on_access_denied']:
                      raise
                  return result

              for page in pages:
                  for volume in page.get('Volumes', []):
                      if settings['max_volumes_per_run'] and (result['would_modify'] + result['modified']) >= settings['max_volumes_per_run']:
                          result['stopped_due_to_max_volumes_per_run'] = True
                          return result

                      result['evaluated'] += 1
                      volume_id = volume.get('VolumeId')
                      volume_type = volume.get('VolumeType')
                      size_gib = volume.get('Size')
                      state = volume.get('State')
                      attachments = volume.get('Attachments', [])
                      is_attached = bool(attachments)
                      tag_map = tags_to_dict(volume.get('Tags', []))

                      if volume_type != 'gp2':
                          result['skipped_non_gp2'] += 1
                          continue

                      result['gp2_volumes'] += 1

                      if size_gib is None or int(size_gib) < settings['minimum_volume_size_gib']:
                          result['skipped_too_small'] += 1
                          continue

                      if is_attached and not settings['include_attached_volumes']:
                          result['skipped_attached_excluded'] += 1
                          continue

                      if not is_attached and not settings['include_unattached_volumes']:
                          result['skipped_unattached_excluded'] += 1
                          continue

                      ignore, ignore_reason = has_ignore_tag(tag_map, settings['ignore_tag_keys'], settings['ignore_tag_pairs'])
                      if ignore:
                          result['skipped_ignore_tag'] += 1
                          result['candidates'].append({
                              'volume_id': volume_id,
                              'size_gib': size_gib,
                              'state': state,
                              'attached': is_attached,
                              'action': 'skipped_ignore_tag',
                              'reason': ignore_reason,
                              'tags': tag_map
                          })
                          continue

                      if not has_approval_tag(tag_map, settings):
                          result['skipped_missing_approval_tag'] += 1
                          result['candidates'].append({
                              'volume_id': volume_id,
                              'size_gib': size_gib,
                              'state': state,
                              'attached': is_attached,
                              'action': 'skipped_missing_approval_tag',
                              'required_tag': settings['approval_tag_key'] + '=' + settings['approval_tag_value'],
                              'tags': tag_map
                          })
                          continue

                      try:
                          active_mod, mod_state = volume_has_active_modification(ec2, volume_id)
                      except ClientError as e:
                          code = e.response.get('Error', {}).get('Code', 'Unknown')
                          message = e.response.get('Error', {}).get('Message', str(e))
                          result['failures'].append({'volume_id': volume_id, 'operation': 'DescribeVolumesModifications', 'error_code': code, 'error_message': message})
                          result['failed'] += 1
                          if not settings['continue_on_access_denied']:
                              raise
                          continue

                      if active_mod:
                          result['skipped_active_modification'] += 1
                          continue

                      target_iops = desired_gp3_iops(volume, settings)
                      target_throughput = settings['target_throughput']

                      result['eligible'] += 1
                      candidate = {
                          'volume_id': volume_id,
                          'size_gib': size_gib,
                          'state': state,
                          'attached': is_attached,
                          'current_type': 'gp2',
                          'target_type': 'gp3',
                          'target_iops': target_iops,
                          'target_throughput': target_throughput,
                          'previous_modification_state': mod_state,
                          'tags': tag_map,
                          'action': 'would_modify' if settings['dry_run'] else 'modify'
                      }

                      if settings['dry_run']:
                          result['would_modify'] += 1
                          result['candidates'].append(candidate)
                          continue

                      try:
                          ec2.modify_volume(
                              VolumeId=volume_id,
                              VolumeType='gp3',
                              Iops=target_iops,
                              Throughput=target_throughput
                          )
                          result['modified'] += 1
                          result['modified_volumes'].append(candidate)
                      except ClientError as e:
                          code = e.response.get('Error', {}).get('Code', 'Unknown')
                          message = e.response.get('Error', {}).get('Message', str(e))
                          result['failures'].append({'volume_id': volume_id, 'operation': 'ModifyVolume', 'error_code': code, 'error_message': message})
                          if code in ('AccessDenied', 'AccessDeniedException', 'UnauthorizedOperation') or 'explicit deny' in message.lower():
                              result['access_denied'] += 1
                          else:
                              result['failed'] += 1
                          if not settings['continue_on_access_denied']:
                              raise

              return result

          def handler(event, context):
              settings = {
                  'excluded_regions': split_csv(os.environ.get('EXCLUDED_REGIONS_CSV', '')),
                  'dry_run': env_bool('DRY_RUN', True),
                  'require_approval_tag': env_bool('REQUIRE_APPROVAL_TAG', True),
                  'approval_tag_key': os.environ.get('APPROVAL_TAG_KEY', 'CostOptApproved'),
                  'approval_tag_value': os.environ.get('APPROVAL_TAG_VALUE', 'true'),
                  'ignore_tag_keys': split_csv(os.environ.get('IGNORE_TAG_KEYS_CSV', '')),
                  'ignore_tag_pairs': parse_tag_pairs(os.environ.get('IGNORE_TAG_PAIRS_CSV', '')),
                  'minimum_volume_size_gib': env_int('MINIMUM_VOLUME_SIZE_GIB', 1),
                  'include_attached_volumes': env_bool('INCLUDE_ATTACHED_VOLUMES', True),
                  'include_unattached_volumes': env_bool('INCLUDE_UNATTACHED_VOLUMES', True),
                  'target_iops': env_int('TARGET_IOPS', 3000),
                  'target_throughput': env_int('TARGET_THROUGHPUT', 125),
                  'preserve_higher_gp2_performance': env_bool('PRESERVE_HIGHER_GP2_PERFORMANCE', True),
                  'max_volumes_per_run': env_int('MAX_VOLUMES_PER_RUN', 50),
                  'continue_on_access_denied': env_bool('CONTINUE_ON_ACCESS_DENIED', True),
              }

              if isinstance(event, dict):
                  if 'dry_run' in event:
                      settings['dry_run'] = bool(event['dry_run'])
                  if 'require_approval_tag' in event:
                      settings['require_approval_tag'] = bool(event['require_approval_tag'])
                  if 'minimum_volume_size_gib' in event:
                      settings['minimum_volume_size_gib'] = int(event['minimum_volume_size_gib'])
                  if 'include_attached_volumes' in event:
                      settings['include_attached_volumes'] = bool(event['include_attached_volumes'])
                  if 'include_unattached_volumes' in event:
                      settings['include_unattached_volumes'] = bool(event['include_unattached_volumes'])
                  if 'target_iops' in event:
                      settings['target_iops'] = int(event['target_iops'])
                  if 'target_throughput' in event:
                      settings['target_throughput'] = int(event['target_throughput'])
                  if 'preserve_higher_gp2_performance' in event:
                      settings['preserve_higher_gp2_performance'] = bool(event['preserve_higher_gp2_performance'])
                  if 'max_volumes_per_run' in event:
                      settings['max_volumes_per_run'] = int(event['max_volumes_per_run'])
                  if 'target_regions' in event and event['target_regions']:
                      os.environ['TARGET_REGIONS_CSV'] = ','.join(event['target_regions'])
                  if 'excluded_regions' in event and event['excluded_regions']:
                      settings['excluded_regions'] = list(event['excluded_regions'])

              summary = {
                  'started_at': utc_now_iso(),
                  'dry_run': settings['dry_run'],
                  'require_approval_tag': settings['require_approval_tag'],
                  'approval_tag': settings['approval_tag_key'] + '=' + settings['approval_tag_value'],
                  'minimum_volume_size_gib': settings['minimum_volume_size_gib'],
                  'include_attached_volumes': settings['include_attached_volumes'],
                  'include_unattached_volumes': settings['include_unattached_volumes'],
                  'target_iops': settings['target_iops'],
                  'target_throughput': settings['target_throughput'],
                  'preserve_higher_gp2_performance': settings['preserve_higher_gp2_performance'],
                  'ignore_tag_keys': settings['ignore_tag_keys'],
                  'ignore_tag_pairs': settings['ignore_tag_pairs'],
                  'max_volumes_per_run': settings['max_volumes_per_run'],
                  'excluded_regions': settings['excluded_regions'],
                  'regions': [],
                  'totals': {
                      'evaluated': 0,
                      'gp2_volumes': 0,
                      'eligible': 0,
                      'would_modify': 0,
                      'modified': 0,
                      'skipped_non_gp2': 0,
                      'skipped_too_small': 0,
                      'skipped_attached_excluded': 0,
                      'skipped_unattached_excluded': 0,
                      'skipped_missing_approval_tag': 0,
                      'skipped_ignore_tag': 0,
                      'skipped_active_modification': 0,
                      'access_denied': 0,
                      'failed': 0
                  },
                  'region_results': []
              }

              try:
                  regions = get_enabled_regions(settings['excluded_regions'])
                  summary['regions'] = regions
                  for region in regions:
                      try:
                          region_result = scan_region(region, settings)
                      except Exception as e:
                          region_result = {
                              'region': region,
                              'evaluated': 0,
                              'gp2_volumes': 0,
                              'eligible': 0,
                              'would_modify': 0,
                              'modified': 0,
                              'skipped_non_gp2': 0,
                              'skipped_too_small': 0,
                              'skipped_attached_excluded': 0,
                              'skipped_unattached_excluded': 0,
                              'skipped_missing_approval_tag': 0,
                              'skipped_ignore_tag': 0,
                              'skipped_active_modification': 0,
                              'access_denied': 0,
                              'failed': 1,
                              'failures': [{'error_code': type(e).__name__, 'error_message': str(e), 'trace': traceback.format_exc()}],
                              'candidates': [],
                              'modified_volumes': []
                          }
                      summary['region_results'].append(region_result)
                      for key in summary['totals']:
                          summary['totals'][key] += int(region_result.get(key, 0) or 0)
              finally:
                  summary['ended_at'] = utc_now_iso()
                  print(json.dumps(summary, indent=2, default=str))
              return summary

  RemediatorScheduleRule:
    Type: AWS::Events::Rule
    Condition: IsScheduleEnabled
    Properties:
      Name: !Sub '${ProjectName}-schedule'
      Description: Scheduled run for EBS gp2 to gp3 Conversion Remediator.
      ScheduleExpression: !Ref ScheduleExpression
      State: ENABLED
      Targets:
        - Arn: !GetAtt RemediatorFunction.Arn
          Id: EbsGp2ToGp3ConversionRemediatorTarget
          Input: '{"source":"eventbridge-schedule"}'

  RemediatorSchedulePermission:
    Type: AWS::Lambda::Permission
    Condition: IsScheduleEnabled
    Properties:
      FunctionName: !Ref RemediatorFunction
      Action: lambda:InvokeFunction
      Principal: events.amazonaws.com
      SourceArn: !GetAtt RemediatorScheduleRule.Arn

Outputs:
  RemediatorFunctionName:
    Description: Lambda function name.
    Value: !Ref RemediatorFunction

  RemediatorFunctionArn:
    Description: Lambda function ARN.
    Value: !GetAtt RemediatorFunction.Arn

  DryRunSetting:
    Description: Current DryRun setting.
    Value: !Ref DryRun

  RequireApprovalTagSetting:
    Description: Current RequireApprovalTag setting.
    Value: !Ref RequireApprovalTag

  TargetIopsSetting:
    Description: Current target gp3 IOPS setting.
    Value: !Ref TargetIops

  TargetThroughputSetting:
    Description: Current target gp3 throughput setting.
    Value: !Ref TargetThroughput

  ManualDryRunExample:
    Description: Example manual dry-run invocation payload.
    Value: !Sub 'aws lambda invoke --function-name ${RemediatorFunction} --cli-binary-format raw-in-base64-out --payload ''{"dry_run":true}'' response.json'

  ManualBatchConversionDryRunExample:
    Description: Example gp2 to gp3 batch conversion dry-run payload.
    Value: !Sub 'aws lambda invoke --function-name ${RemediatorFunction} --cli-binary-format raw-in-base64-out --payload ''{"dry_run":true,"require_approval_tag":false,"max_volumes_per_run":50}'' response.json'
